package com.example.mywheelview;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.common.WheelData;
import com.wx.wheelview.widget.WheelView;
import com.wx.wheelview.widget.WheelViewDialog;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout.LayoutParams;

public class MyPop extends PopupWindow{
	private WheelView mainWheelView, subWheelView, childWheelView;
	private View conentView;  
    public MyPop(final Activity context) {  
        LayoutInflater inflater = (LayoutInflater) context  
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        conentView = inflater.inflate(R.layout.mypop_dialog, null); 
        initWheel1(context,conentView);
        int h = context.getWindowManager().getDefaultDisplay().getHeight();  
        int w = context.getWindowManager().getDefaultDisplay().getWidth();  
        // 设置SelectPicPopupWindow的View  
        this.setContentView(conentView);  
        // 设置SelectPicPopupWindow弹出窗体的宽  
        this.setWidth(w);  
        // 设置SelectPicPopupWindow弹出窗体的高  
        this.setHeight(LayoutParams.WRAP_CONTENT);  
        // 设置SelectPicPopupWindow弹出窗体可点击  
        this.setFocusable(true);  
        this.setOutsideTouchable(true);  
        // 刷新状态  
        this.update();  
        // 实例化一个ColorDrawable颜色为半透明  
        ColorDrawable dw = new ColorDrawable(0000000000);  
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作  
        this.setBackgroundDrawable(dw);  
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);  
        // 设置SelectPicPopupWindow弹出窗体动画效果  
//        this.setAnimationStyle(R.style.AnimationPreview);  
        
    }  
  
    public void showPopupWindow(View parent) {  
        if (!this.isShowing()) {  
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);  
            
        } else {  
            this.dismiss();  
        }  
    } 
    /**
	 * 联动WheelView
     * @param conentView2 
     * @param context 
	 */
	public void initWheel1(Activity context, View conentView2) {
		mainWheelView = (WheelView) conentView2.findViewById(R.id.main_wheelview);
		mainWheelView.setWheelAdapter(new ArrayWheelAdapter(context));
		mainWheelView.setSkin(WheelView.Skin.Holo);
		mainWheelView.setWheelData(createMainDatas());
		WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
		style.selectedTextSize = 20;
		style.textSize = 16;
		mainWheelView.setStyle(style);

		subWheelView = (WheelView) conentView2.findViewById(R.id.sub_wheelview);
		subWheelView.setWheelAdapter(new ArrayWheelAdapter(context));
		subWheelView.setSkin(WheelView.Skin.Holo);
		subWheelView.setWheelData(createSubDatas().get(createMainDatas().get(mainWheelView.getSelection())));
		subWheelView.setStyle(style);
		mainWheelView.join(subWheelView);
		mainWheelView.joinDatas(createSubDatas());

		childWheelView = (WheelView) conentView2.findViewById(R.id.child_wheelview);
		childWheelView.setWheelAdapter(new ArrayWheelAdapter(context));
		childWheelView.setSkin(WheelView.Skin.Holo);
		childWheelView.setWheelData(createChildDatas().get(createSubDatas()
				.get(createMainDatas().get(mainWheelView.getSelection())).get(subWheelView.getSelection())));
		childWheelView.setStyle(style);
		subWheelView.join(childWheelView);
		subWheelView.joinDatas(createChildDatas());
	}

	private List<String> createMainDatas() {
		String[] strings = { "黑龙江", "吉林", "辽宁" };
		return Arrays.asList(strings);
	}

	private HashMap<String, List<String>> createSubDatas() {
		HashMap<String, List<String>> map = new HashMap<String, List<String>>();
		String[] strings = { "黑龙江", "吉林", "辽宁" };
		String[] s1 = { "哈尔滨", "齐齐哈尔", "大庆" };
		String[] s2 = { "长春", "吉林" };
		String[] s3 = { "沈阳", "大连", "鞍山", "抚顺" };
		String[][] ss = { s1, s2, s3 };
		for (int i = 0; i < strings.length; i++) {
			map.put(strings[i], Arrays.asList(ss[i]));
		}
		return map;
	}

	private HashMap<String, List<String>> createChildDatas() {
		HashMap<String, List<String>> map = new HashMap<String, List<String>>();
		String[] strings = { "哈尔滨", "齐齐哈尔", "大庆", "长春", "吉林", "沈阳", "大连", "鞍山", "抚顺" };
		String[] s1 = { "道里区", "道外区", "南岗区", "香坊区" };
		String[] s2 = { "龙沙区", "建华区", "铁锋区" };
		String[] s3 = { "红岗区", "大同区" };
		String[] s11 = { "南关区", "朝阳区" };
		String[] s12 = { "龙潭区" };
		String[] s21 = { "和平区", "皇姑区", "大东区", "铁西区" };
		String[] s22 = { "中山区", "金州区" };
		String[] s23 = { "铁东区", "铁西区" };
		String[] s24 = { "新抚区", "望花区", "顺城区" };
		String[][] ss = { s1, s2, s3, s11, s12, s21, s22, s23, s24 };
		for (int i = 0; i < strings.length; i++) {
			map.put(strings[i], Arrays.asList(ss[i]));
		}
		return map;
	}

	private ArrayList<String> createHours() {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i < 24; i++) {
			if (i < 10) {
				list.add("0" + i);
			} else {
				list.add("" + i);
			}
		}
		return list;
	}

	private ArrayList<String> createMinutes() {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i < 60; i++) {
			if (i < 10) {
				list.add("0" + i);
			} else {
				list.add("" + i);
			}
		}
		return list;
	}

	private ArrayList<String> createArrays() {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i < 20; i++) {
			list.add("item" + i);
		}
		return list;
	}

	private ArrayList<WheelData> createDatas() {
		ArrayList<WheelData> list = new ArrayList<WheelData>();
		WheelData item;
		for (int i = 0; i < 20; i++) {
			item = new WheelData();
			item.setId(R.drawable.ic_launcher);
			item.setName((i < 10) ? ("0" + i) : ("" + i));
			list.add(item);
		}
		return list;
	}

}
